package com.dongdongshop.controller;

import cn.hutool.json.JSONUtil;
import com.dongdongshop.data.Result;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbSpecificationOption;
import com.dongdongshop.service.SpecService;
import com.dongdongshop.vo.SpecVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@RequestMapping("spec")
@Controller
public class SpecController {

    @DubboReference
    private SpecService specService;

    @RequestMapping("toSpec")
    public String toSpec(){
        return "/admin/specification";
    }

    @RequestMapping("listSpec")
    @ResponseBody
    public Result listSpec(){
        try {
            List<TbSpecification> tbSpecifications = specService.listSpec();
            return Result.OK().setData(tbSpecifications);
        }catch (Exception e){
            e.printStackTrace();
            return Result.ER();
        }
    }

    @RequestMapping("addSpec")
    @ResponseBody
    public Result addSpec(String specName,String arrstr){
        List<TbSpecificationOption> tbSpecificationOptions = JSONUtil.toList(arrstr, TbSpecificationOption.class);
        try {
            specService.addSpec(specName,tbSpecificationOptions);
            return Result.OK();
        }catch (Exception e){
            e.printStackTrace();
            return Result.ER();
        }
    }
    @RequestMapping("toUpdate")
    @ResponseBody
    public Result toUpdate(Long id){
        try {
            SpecVo specVo = specService.toUpdate(id);
            return Result.OK().setData(specVo);
        }catch (Exception e){
            e.printStackTrace();
            return Result.ER();
        }
    }
    @RequestMapping("update")
    @ResponseBody
    public Result update(Long id,String specName,String arrstr){
        List<TbSpecificationOption> tbSpecificationOptions = JSONUtil.toList(arrstr, TbSpecificationOption.class);
        try {
            specService.update(id,specName,tbSpecificationOptions);
            return Result.OK();
        }catch (Exception e){
            e.printStackTrace();
            return Result.ER();
        }

    }
    @RequestMapping("delSpecs")
    @ResponseBody
    public Result delSpecs(@RequestParam(value = "ids") List<Long> ids){
        try {
            specService.delSpecs(ids);
            return Result.OK();
        }catch (Exception e){
            e.printStackTrace();
            return Result.ER();
        }
    }
}
